package com.example.myprojectoffeixue;

import android.animation.ObjectAnimator;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import org.java_websocket.client.WebSocketClient;

import java.util.ArrayList;

/**
 * Created by ASUS on 2017/1/4.
 */
public class CurtainAdapter extends BaseAdapter{
    private int current = 1;
    private ArrayList<SheBeiClass> d;
    private LayoutInflater layoutInflater;
    private Context context;
    WebSocketClient wsc;
    String cmd;
    private CurtainActivity mg;
    private String ieee;
    private String port;
    private String mac;
    private String s;
    String j;
    public AutoButton autoButton;
    private TextView bt;
    private TextView bt1;
    private TextView bt2;
    private Dialog dialog1;
    private final MyApplication m;
    public LinearLayout le;


    public CurtainAdapter(ArrayList<SheBeiClass> d, Context context, String cmd) {
        super();
        this.d = d;
        this.context = context;
        this.cmd = cmd;
        mg = (CurtainActivity) context;
        m = (MyApplication) mg.getApplication();
        this.layoutInflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return d.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return d.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder h = null;
        if (convertView == null) {
            h = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.item_curtain, null);
            h.tv = (Button) convertView.findViewById(R.id.tv);
            h.rg = (RadioGroup) convertView.findViewById(R.id.ll_daily_recipe_footer);
            h.rb1 = (RadioButton) convertView.findViewById(R.id.rb_signin);
            h.rb2=(RadioButton) convertView.findViewById(R.id.rb_leave);
            h.rb3=(RadioButton) convertView.findViewById(R.id.rb_unsignin);
            h.name= (TextView) convertView.findViewById(R.id.tv_listview);
            convertView.setTag(h);
        } else {
            h = (ViewHolder) convertView.getTag();
        }
        h.name.setText(d.get(position).deviceName);
        h.tv.setLongClickable(true);
        h.tv.setOnTouchListener(new MyGestureListener(context,h.tv,position));

        if("01".equals(d.get(position).status)){


            Log.d("kai","kai");
            translationX(dp2px(context.getResources(), -50), h.tv);

        }else if("00".equals(d.get(position).status)){

            Log.d("guan","kai");

            translationX(dp2px(context.getResources(),50),h.tv);


        }else{

            Log.d("ting","kai");

            translationX(dp2px(context.getResources(), 0), h.tv);
        }
        final ViewHolder finalH = h;
        h.rb1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wsc = m.c;
                if (d.size() != 0) {
                    s = "01";
                    Kuaiguanutil k = new Kuaiguanutil();
                    k.ieee = d.get(position).ieee;
                    k.port = d.get(position).port;
                    k.status = s;
                    k.cmd = "1008";
                    k.mac = d.get(position).mac;
                    j = JSON.toJSONString(k);
                    Log.d("cmd", j);
                    if (m.flag3) {
                        wsc.send(j);
                    }
                    d.get(position).status = "01";
                    current = 0;
                    translationX(dp2px(context.getResources(), -50), finalH.tv);

                }
            }
        });

        h.rb2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wsc = m.c;
                if (d.size() != 0) {
                    s = "02";
                    Kuaiguanutil k = new Kuaiguanutil();
                    k.ieee = d.get(position).ieee;
                    k.port = d.get(position).port;
                    k.cmd = "1008";
                    k.status = s;
                    k.mac = d.get(position).mac;
                    j = JSON.toJSONString(k);
                    Log.d("cmd", j);
                    if (m.flag3) {
                        wsc.send(j);
                    }
                    d.get(position).status = "02";
                    current = 1;
                    translationX(dp2px(context.getResources(), 0), finalH.tv);
                }

            }
        });

        h.rb3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                wsc = m.c;
                if (d.size() != 0) {
                    s = "00";
                    Kuaiguanutil k = new Kuaiguanutil();
                    k.ieee = d.get(position).ieee;
                    k.port = d.get(position).port;
                    k.cmd="1008";
                    k.status=s;
                    k.mac=d.get(position).mac;
                    j = JSON.toJSONString(k);
                    Log.d("cmd",j);
                    if (m.flag3) {
                        wsc.send(j);
                    }
                    d.get(position).status = "00";
                current = 2;
                translationX(dp2px(context.getResources(),50),finalH.tv);}

            }
        });

        return convertView;



    }

    class ViewHolder {
        Button tv;
        RadioGroup rg;
        RadioButton rb1;
        RadioButton rb2;
        RadioButton rb3;
        TextView name;


    }

    class MyGestureListener extends GestureListener {
        Button tv;
        int position;
        public MyGestureListener(Context context,Button tv,int position) {
            super(context);
            this.tv=tv;
            this.position=position;
        }

        @Override
        public boolean left() {
            Log.d("dataleft",current+"");
            switch (current) {
                case 0:
                    wsc = m.c;
                    if (d.size() != 0) {
                        s = "01";
                        Kuaiguanutil k = new Kuaiguanutil();
                        k.ieee = d.get(position).ieee;
                        k.port = d.get(position).port;
                        k.status = s;
                        k.cmd = "1008";
                        k.mac = d.get(position).mac;
                        j = JSON.toJSONString(k);
                        Log.d("cmd", j);
                        if (m.flag3) {
                            wsc.send(j);
                        }
                        d.get(position).status = "01";
                        current =0;
                        translationX(dp2px(context.getResources(), -50),tv);

                    }

                    break;
                case 1:
                    wsc = m.c;
                    if (d.size() != 0) {
                        s = "01";
                        Kuaiguanutil k = new Kuaiguanutil();
                        k.ieee = d.get(position).ieee;
                        k.port = d.get(position).port;
                        k.cmd = "1008";
                        k.status = s;
                        k.mac = d.get(position).mac;
                        j = JSON.toJSONString(k);
                        Log.d("cmd", j);
                        if (m.flag3) {
                            wsc.send(j);
                        }

                        d.get(position).status = "01";

                        current = 0;
                        translationX(dp2px(context.getResources(), -50), tv);


                    }

                    break;
                case 2:

                    wsc = m.c;
                    if (d.size() != 0) {
                        s = "02";
                        Kuaiguanutil k = new Kuaiguanutil();
                        k.ieee = d.get(position).ieee;
                        k.port = d.get(position).port;
                        k.cmd = "1008";
                        k.status = s;
                        k.mac = d.get(position).mac;
                        j = JSON.toJSONString(k);
                        Log.d("cmd", j);
                        if (m.flag3) {
                            wsc.send(j);
                        }
                        d.get(position).status = "02";
                        current = 1;
                        translationX(dp2px(context.getResources(), 0), tv);
                    }
                    break;

            }
            return super.left();
        }

        @Override
        public boolean right() {
            Log.e("test", ""+current);
            switch (current){
                case 0:
                    wsc = m.c;
                    if (d.size() != 0) {
                        s = "02";
                        Kuaiguanutil k = new Kuaiguanutil();
                        k.ieee = d.get(position).ieee;
                        k.port = d.get(position).port;
                        k.status = s;
                        k.cmd = "1008";
                        k.mac = d.get(position).mac;
                        j = JSON.toJSONString(k);
                        Log.d("cmd", j);
                        if (m.flag3) {
                            wsc.send(j);
                        }
                        d.get(position).status = "02";
                        current = 1;
                       translationX(dp2px(context.getResources(),0), tv);}

                    break;
                case 1:
                    wsc = m.c;
                    if (d.size() != 0) {
                        s = "00";
                        Kuaiguanutil k = new Kuaiguanutil();
                        k.ieee = d.get(position).ieee;
                        k.port = d.get(position).port;
                        k.cmd = "1008";
                        k.status = s;
                        k.mac = d.get(position).mac;
                        j = JSON.toJSONString(k);
                        Log.d("cmd", j);
                        if (m.flag3) {
                            wsc.send(j);
                        }
                        d.get(position).status = "00";
                    current = 2;
                    translationX(dp2px(context.getResources(),50), tv);}

                    break;
                case 2:
                    wsc = m.c;
                    if (d.size() != 0) {
                        s = "00";
                        Kuaiguanutil k = new Kuaiguanutil();
                        k.ieee = d.get(position).ieee;
                        k.port = d.get(position).port;
                        k.cmd = "1008";
                        k.status = s;
                        k.mac = d.get(position).mac;
                        j = JSON.toJSONString(k);
                        Log.d("cmd", j);
                        if (m.flag3) {
                            wsc.send(j);
                        }
                        d.get(position).status = "00";
                    }

                    break;
            }
            return super.right();
        }
    }

    private void translationX(float nextTranslationX,Button tv){
        float curTranslationX = tv.getTranslationX();
        ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "translationX", curTranslationX, nextTranslationX);
        animator.setDuration(0);
        animator.start();
    }
    public static float dp2px(Resources resources, float dp) {
        final float scale = resources.getDisplayMetrics().density;
        return  dp * scale + 0.5f;
    }












}
